package com.sxt.demo.djiapplication;

import android.app.Application;
import android.content.Context;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.cookie.store.PersistentCookieStore;
import com.secneo.sdk.Helper;
import com.sxt.demo.djiapplication.dji.DJIDemoContext;
import com.sxt.demo.djiapplication.exceptionhandler.CrashHandler;
import com.sxt.demo.djiapplication.utils.DbManager;
import com.sxt.demo.djiapplication.utils.ToastUtils;
import com.xuexiang.xui.XUI;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.util.concurrent.ConcurrentHashMap;


public class DjiDemoApplication extends Application {

    private DJIDemoContext djiDemoContext;

    public static String TMP_FILE_DIR = "";

    private static final ConcurrentHashMap<Class<?>, Object> singleObjs = new ConcurrentHashMap<>();

    @Override
    protected void attachBaseContext(Context paramContext) {
        super.attachBaseContext(paramContext);
        Helper.install(DjiDemoApplication.this);
        if (djiDemoContext == null) {
            djiDemoContext = new DJIDemoContext(this);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();

        TMP_FILE_DIR = getApplicationContext().getFilesDir().getAbsolutePath() + "/tmp_files/";

        // 初始化dji上下文
        djiDemoContext.onCreate();

        // 初始化ToastUtils
        ToastUtils.init(this);

        //初始化UI框架
        XUI.init(this);
        //开启UI框架调试日志
        XUI.debug(true);

        // 初始化greenDao数据库操作工具
        DbManager.initDb(this);

        // 初始化okHttp工具
        initOkHttpUtils();

        // 初始化崩溃处理handler
        CrashHandler.getInstance().init(this);

        test();
    }

    /**
     * 项目开发时的测试代码
     */
    private void test() {
//        FlightDataService flightDataService = new FlightDataService(getApplicationContext());
//        flightDataService.addFlightData(new FlightDataAddParam(0.1f, 0.3f, 3.2f, 1, 23, 1.3, 232.3, 2.2f, 1.2f));
//        flightDataService.addFlightData(new FlightDataAddParam(0.1f, 0.3f, 3.2f, 1, 23, 1.3, 232.3, 2.2f, 1.2f));
//        flightDataService.addFlightData(new FlightDataAddParam(0.1f, 0.3f, 3.2f, 1, 23, 1.3, 232.3, 2.2f, 1.2f));
//        flightDataService.addFlightData(new FlightDataAddParam(0.1f, 0.3f, 3.2f, 1, 23, 1.3, 232.3, 2.2f, 1.2f));
//        flightDataService.addFlightData(new FlightDataAddParam(0.1f, 0.3f, 3.2f, 1, 23, 1.3, 232.3, 2.2f, 1.2f));
//        flightDataService.addFlightData(new FlightDataAddParam(0.1f, 0.3f, 3.2f, 1, 23, 1.3, 232.3, 2.2f, 1.2f));
//        flightDataService.addFlightData(new FlightDataAddParam(0.1f, 0.3f, 3.2f, 1, 23, 1.3, 232.3, 2.2f, 1.2f));
//        flightDataService.addFlightData(new FlightDataAddParam(0.1f, 0.3f, 3.2f, 1, 23, 1.3, 232.3, 2.2f, 1.2f));
//        flightDataService.addFlightData(new FlightDataAddParam(0.1f, 0.3f, 3.2f, 1, 23, 1.3, 232.3, 2.2f, 1.2f));
//        new FlightDataService(getApplicationContext()).addFlightData(new FlightDataAddParam(0.1f, 0.3f, 3.2f, 1, 23, 1.3, 232.3, 2.2f, 1.2f));
//        DbManager.getFlightDataService().add(new FlightData(0.1f, 0.3f, 3.2f, 1, 23, 1.3, 232.3, 2.2f, 1.2f));
    }

    /**
     * 初始化OkHttpUtils
     */
    private void initOkHttpUtils() {
        OkHttpUtils.init(this);
        OkHttpUtils.getInstance()
                //打开该调试开关,控制台会使用 红色error 级别打印log,并不是错误,是为了显眼,不需要就不要加入该行
                .debug("OkHttpUtils")
                .setConnectTimeout(OkHttpUtils.DEFAULT_MILLISECONDS)  //全局的连接超时时间
                .setReadTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS)     //全局的读取超时时间
                .setWriteTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS)    //全局的写入超时时间
                .setCacheMode(CacheMode.NO_CACHE)
                .setCookieStore(new PersistentCookieStore());
    }

    public static <T> T getSingleObj(Class<T> tClass) {
        Object obj = singleObjs.get(tClass);
        if (obj == null) {
            try {
                obj = tClass.newInstance();
                singleObjs.put(tClass, obj);
            } catch (IllegalAccessException | InstantiationException e) {
                e.printStackTrace();
            }
        }
        //noinspection unchecked
        return (T) obj;
    }

}